/**
* A Library v1.0.0
* Author Muriel Ziqi
* Date: 2018-7-20
*/
~(function(window){
	/***
	* @name  框架单体对象 A
	* @param selector 选择器或页面加载回调函数
	* @param context  查找元素上下文
	**/
	var A = function(selector, context){
		//如果selector为方法则为窗口添加页面加载完成事件监听
		if(typeof selector == 'function'){
			A(window).on('load', selector);
		}else{
			//创建A对象
			return new A.fn.init(selector, context);
		}
	}
	//原型方法
	A.fn = A.prototype = {
		//强化构造函数
		constructor : A,
		//构造函数
		init : function(selector, context){
			//modify选择器为元素
			if(typeof selector === 'object'){
				this[0] = selector;
				this.length = 1;
				return this;
			};
			//设置获取到元素长度属性
			this.length = 0,
			//矫正上下文
			context =document.getElementById(context) || document;
			//如果是id选择器
			if(~selector.indexOf('#')){
				this[0] = document.getElementById(selector.slice(1));
				this.length = 1;
				//如果是类选择器
			}else if(~selector.indexOf('.')){
				var doms = [],
				className = selector.slice(1);
				//支持通过类获取元素的方法
				if(context.getElementsByClassName){
					doms = context.getElementsByClassName(className);
				}else{
					doms = context.getElementsByTagName('*');
				}
				//设置获取到的元素
				for (var i = 0, len = doms.lenth; i < len; i++){
					if(doms[i].className && !!~doms[i].className.indexOf(className)){
						this[this.length] = doms[i];
						//矫正长度
						this.length++;
					}
				}
				//否则为元素名选择器
			}else{
				var doms = context.getElementsByTagName(selector),
				i = 0,
				len = doms.length;
				for(; i < len; i++){
					this[i] = doms[i];
				}
				this.length = len;
			}
			//设置当前对象的选择上下文
			this.context = context;
			//设置当前对象的选择器
			this.selector = selector;
			return this;
		},
		//元素长度
		length : 0,
		//增强数组
		push: [].push,
		splice: [].splice
	}
	//设置构造函数原型
	A.fn.init.prototype = A.fn;
	/***
	* @name         对象拓展
	* @param[0]     目标对象
	* @param[1,...] 拓展对象
	**/
	A.extend = A.fn.extend = function(){
		var i = 1,
		    len = arguments.length,
		    target = arguments[0],
		    j;
		//如果一个参数，则为当前对象拓展方法
		if(i == len){
			target = this;
			i--;
		}
		//遍历拓展对象
		for(; i < len; i++){
			//遍历拓展对象中方法与属性
			for(j in arguments[i]){
				//浅复制
				target[j] = arguments[i][j];
			}
		}
		//返回目标对象
		return target;
	};
	//单体对象A方法拓展
	A.extend({
		/***
		* @name 将横线命名字符串转化为驼峰式
		* eg : 'test-demo' -> 'testDemo'
		**/
		camelCase : function(str){
			return str.replace(/\-(\w)/g, function(match, letter){
				return letter.toUpperCase();
			});
		},
		/***
		* @name 去除字符串两端空白符
		* eg ： 't es t' -> 't es t'
		**/
		trim : function(){
			return str.replace(/^\s+|\s+$/g, '')
		},
		/***
		* @ name 创建一个元素并包装成A对象
		* @param type 元素类型
		* @param value 元素属性对象
		**/
		create : function(type, value){
			var dom = document.createElement(type);
			return A(dom).arr(value);
		},
		/***
		* @name    格式化模板
		* @param   str           模板字符串
		* @param   data          渲染数据
		* eg： '<div>{#value#}</div>' + {value:'test'} -> '<div>test</diV>'
		**/
		formateString : function(str, data){
			var html = '';
			if(data instanceof Array){
				for(var i = 0, len = data.length; i < len; i++){
					html += arguments.callee(str, data[i]);
				}
				return html;
			}else{
				return str.replace(/\{#(\w+)#}/g, function(match, key){
					return typeof data ==="string" ? data : (typeof fata[key] === 'undefined' ? '' : data[key])
				});
			}
		}
	});
	//事件绑定方法
	var _on = (function(){
		if(document.addEventListener){
			return function(dom, type, fn, data){
				dom.addEventListener(type, function(e){
					fn.call(dom, e, data);
				}, false);
			}
		}else if(document.attachEvent){
			return function(dom, type, fn, data){
				dom.attachEvent('on' + type, function(e){
					fn.call(dom, e, data);
				});
			}
		}else{
			return function(dom, type, fn, data){
				dom['on' + type] = function(e){
					fn.call(dom, e, data);
				};
			}
		}
	})();
	A.fn.extend({
		on : function(type, fn, data){
			var i = this.length;
			for(; --i >= 0;){
				_on(this[i], type, fn, data);
			}
			return this;
		},
		css : function(){
			var arg = arguments,
				len = arg.length;
				if(this.length < 1){
					return this;
				}
				if(len === 1){
					if(typeof arg[0] === 'string'){
						if(this[0].currentStyle){
							return this[0].currentStyle[name];
						}else {
							return getComputedStyle(this[0],false)[name];
						}
					}else if(typeof arg[0] === 'object'){
						for(var i in arg[0]){
							for(var j = this.length - 1; j >= 0; j--){
								this[j].style[A.camelCase(i)] = arg[0][i];
							}
						}
					}
				}else if(len === 2){
					for(var j = this.length - 1; j >= 0; j--){
						this[j].style[A.camelCase(arg[0])] = arg[1];
					}
				}
				return this;
		},
		attr : function(){
			var arg = arguments,
				len = arg.length;
			if(this.length < 1){
				return this;
			}
			if(len === 1){
				if(typeof arg[0] === 'string'){
					return this[0].getAttribute(arg[0]);
				}else if(typeof arg[0] === 'object'){
					for(var i in arg[0]){
						for(var j = this.length -1; j >= 0; j--){
							this[j].setAttribute(i, arg[0][i]);
						}
					}
				}
			}else if(len === 2){
				for(var j = this.length - 1; j >= 0; j--){
					this[j].setAttribute(arg[0], arg[1]);
				}
			}
			return this;
		},
		html : function(){
			var arg = arguments,
				len = arg.length;
				if(this.length < 1){
					return this;
				}
				if(len === 0){
					return this[0].innerHTML;
				}else if(len === 1){
					for(var i = this.length - 1; i >= 0; i--){
						this[i].innerHTML = arg[0];
					}
				}else if(len === 2 && arg[1]){
					for(var i = this.length - 1; i >= 0; i--){
						this[i].innerHTML += arg[0];
					}
				}
				return this;
		},
		/***
		 * @name 判断类存在
		 * @param val 类名
		 **/
		hasClass : function(val){
			if(!this[0]){
				return;
			}
			var value = A.trim(val);
			return this[0].className&&this[0].className.indexOf(value) >= 0 ? true : false;
		},
		/***
		 * @name 添加类
		 * @param val 类名
		 **/
		addClass : function(val){
			var value = A.trim(val),
				str = '';
			for(var i = 0, len = this.length; i < len; i++){
				str = this[i].className;
				if(!~str.indexOf(value)){
					this[i].className += ' ' + value;
				}
			}
			return this;
		},
		/***
		 * @name 移除类
		 * @param val 类名
		 **/
		removeClass : function(val){
			var value = A.trim(val),
				classNameArr,
				result;
			for(var i = 0, len = this.length; i < len; i++){
				if(this[i].className && ~this[i].className.indexOf(value)){
					classNameArr = this[i].className.split(' ');
					result = '';
					for(var j = classNameArr.length - 1; j >= 0; j--){
						classNameArr[j] = A.trim(classNameArr);
						result += classNameArr[j]&&classNameArr[j]!=value?''+classNameArr[j]:'';
					}
					this[i].className = result;
				}
			}
			return this;
		},
		/***
		 * @name 移除类
		 * @param val 类名
		 **/
		appendTo : function(parent){
			var doms = A(parent);
			if(doms.length){
				for(var j = this.length - 1; j >= 0; j--){
					doms[0].appendChild(this[j]);
				}
			}
		}
	});

	// 运动框架单体对象
	var Tween = {
		timer : 0,
		queen : [],
		interval : 13,
		easing : {
			def : function (time, startValue, changeValue, duration){
				return changeValue * time / duration + startValue
			},
			easeOutQuart : function(time, startValue, changeValue, duration){
				return - changeValue * ((time=time/duration-1)*time*time-1)+startValue;
			}
		},
		/***
		 * @name 添加运动成员
		 * @param val 运动成员
		 **/
		add : function(instance){
			this.queen.push(instance);
			this.run();
		},
		/***
		 * @name 停止框架运行
		 **/
		clear : function(){
			if(this.timer)
				return;
			this.clear();
			this.timer = setInterval(this.loop, this.interval);
		},
		/***
		 * @name 运动框架循环方法
		 **/
		loop : function(){
			if(Tween.queen.length === 0){
				Tween.clear();
				return;
			}
			var now =+ new Date();
			for(var i = Tween.queen.length - 1; i >= 0; i--){
				var instance = Tween.queen[i];
				instance.passed = now - instance.start;
				if(instance.passed < instance.duration){
					Tween.workFn(instance);
				}else{
					Tween.endFn(instance);
				}
			}
		},
		/***
		 * @name 运动方法
		 * @param instance 运动成员
		 **/
		workFn : function(instance){
			instance.tween = this.easing[instance.type](instance.passed, instance.from,
				 instance.to - instance.from, instance.duration);
				 this.exec(instance);
		},
		/***
		 * @name 结束方法
		 * @param instance 运动成员
		 **/
		endFn : function(instance){
			instance.passed = instance.duration;
			instance.tween = instance.to;
			this.exec(instance);
			this.distory(instance);
		},
		/***
		 * @name 执行主函数
		 * @param instance 运动成员
		 **/
		exec : function(instance){
			instance.end();
			this.queen.splice(this.queen.indexOf(instance), 1);
			for(var i in instance){
				delete instance[i];
			}
		},

	}

		/***
		 * @name 获取当前成员在运动成员中的位置
		 * @param instance 运动成员
		 **/
		Tween.queen.indexOf = function(){
			var that = this;
			return Tween.queen.indexOf || function(instance){
				for(var i = 0, len = that.length; i < len; i++){
					if(that[i] === instance){
						return i;
					}
				}
				return -1;
			}
		}();
	
		A.fn.extend({
		/***
		 * @name  动画模块
		 * @param obj 动画成员对象
		 **/				
			animate : function(obj){
				var obj = A.extend({
					duration : 400,
					type : 'def',
					from : 0,
					to : 1,
					start : +new Date(),
					dom : this,
					main : function(){},
					end : function(){},
				}, obj);
				Tween.add(obj);
			}
		});
		/***
		 * @name  避免框架别名冲突（主要用于页面中引入多核框架）
		 * @param library 其他框架
		 **/
		A.noConflict = function(library){
			if(library){
				window.$ = library;
			}else{
				window.$ = null;
				delete window.$;
			}
			return A;
		}
		window.$ = window.A = A;
	// })(window);
})(window);
	// 	window.$ = window.A = A;
	// })(window)